import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;

/** 
 * Some of this code was modified from code provided by Hirondelle Systems under the
 * BSD License.  Code was obtained from http://www.javapractices.com/topic/TopicAction.do?Id=87.
 * This is an appropriate use of said code under the provisions of the BSD License.
 * 
 * @author CJ Buice
 * @version 2011.0426
 * @since 1.6
 *
 */
public class ParseCSV {

	/**
	 * Constructor for ParseCSV class.
	 * 
	 * Opens the input .csv file, sets private applicants variable equal to the
	 * one passed from the calling method. 
	 * 
	 * @param appArray
	 */
	public ParseCSV(ArrayList<String[]> appArray) {
		fFile = new File("Test_Case.csv");
		applicants = appArray;
	}
	
	/**
	 * Uses a Scanner object to break input file into lines, then calls processLine() for each line
	 * in order to process them and add them to the applicants ArrayList.
	 * 
	 * @throws FileNotFoundException
	 */
	public final void processLineByLine() throws FileNotFoundException {
		Scanner scanner = new Scanner(new FileReader(fFile));
		try {
			while (scanner.hasNextLine()) {
				processLine( scanner.nextLine() + "," );
			}
		}
		finally {
			scanner.close();
		}
	}
	
	/**
	 * Takes a one-line String object, breaks it into smaller Strings using "," as a delimiter
	 * (because it is a .csv file). The divided Strings are then trimmed, converted to lower case
	 * for purposes of easy comparisons, puts them into an array of Strings, then passes that 
	 * array to the add() function.
	 * 
	 * @param aLine A string, one line from the input .csv file being processed.
	 */
	protected void processLine(String aLine) {
		Scanner scanner = new Scanner(aLine);
		scanner.useDelimiter(",");
		if(scanner.hasNext()) {
			String name = scanner.next();
			String trip1 = scanner.next();
			String trip2 = scanner.next();
			String trip3 = scanner.next();
			String trip4 = scanner.next();
			String trip5 = scanner.next();
			String trip6 = scanner.next();
			String trip7 = scanner.next();
			String trip8 = scanner.next();
			String trip9 = scanner.next();
			String trip10 = scanner.next();
			String[] lineArr = {name.trim(), trip1.trim().toLowerCase(), trip2.trim().toLowerCase(), 
					trip3.trim().toLowerCase(), trip4.trim().toLowerCase(), trip5.trim().toLowerCase(),
					trip6.trim().toLowerCase(), trip7.trim().toLowerCase(), trip8.trim().toLowerCase(),
					trip9.trim().toLowerCase(), trip10.trim().toLowerCase()};
			add(lineArr);
		}
	}
	
	private final File fFile;
	private static ArrayList<String[]> applicants;
	
	/**
	 * Takes an array consisting of the divided strings from one row (or line) of the input .csv
	 * file and adds them to the applicants ArrayList for later processing.
	 * 
	 * @param lineArr
	 */
	private static void add(String[] lineArr) {
		applicants.add(lineArr);
	}
}
